package learn.linkedlist;

public class DoubleLinkedListDemo {
}

class DoubleLinkedList {
    //初始化head
    public final HeroNode2 head = new HeroNode2(0, "", "");

    public HeroNode2 getHead() {
        return head;
    }

    public void list() {
        if (head.next == null) {
            return;
        }
        HeroNode2 temp = head.next;
        while (true) {
            System.out.println(temp.toString());
            if (temp.next == null) {
                break;
            }
            //后移temp
            temp = temp.next;
        }
    }

    public void add(HeroNode2 node) {
        //因为head不能动，因此需要一个赋值变量
        HeroNode2 temp = head;
        //遍历链表取到最后
        while (temp.next != null) {
            temp = temp.next;
        }
        //退出循环时，temp为链表的末尾
        temp.next = node;
        node.pre = temp;
    }

    /**
     * 和单项链表一样
     *
     * @param node
     */
    public void update(HeroNode2 node) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //辅助节点
        HeroNode2 temp = head.next;

        boolean flag = false;

        while (true) {
            if (temp == null) {
                //链表已经遍历完成
                break;
            }
            if (temp.no == node.no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        System.out.println("原节点为：" + temp.toString());
        temp.name = node.name;
        temp.nickname = node.nickname;
        System.out.println("修改后的节点为：" + temp.toString());
    }

    public void delete(int no) {
        if (head.next == null) {
            System.out.println("链表为空");
            return;
        }
        //辅助节点
        HeroNode2 temp = head.next;
        //是否找到需要删除的节点
        boolean flag = false;
        boolean isLast = false;
        while (true) {
            if (temp == null) {//循环到最后了
                break;
            }
            if (temp.next == null) {
                isLast = true;
            }
            if (temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.pre.next = temp.next;
            if (!isLast) {//要删除的节点不是最后一个
                temp.next.pre = temp.pre;
            }
        } else {
            System.out.println("未找到需要删除的节点");
        }
    }

    public void addByOrder(HeroNode2 node) {
        if (head.next == null) {
            head.next = node;
            node.pre = head;
            return;
        }
        HeroNode2 temp = head.next;
        boolean isLast = false;
        while (true) {
            if (temp.next == null) {
                isLast = true;
                break;
            }
            if (temp.no > node.no) {
                break;
            }
            temp = temp.next;
        }
        if (isLast) {
            if (temp.no < node.no) {
                temp.next = node;
                node.pre = temp;
            } else {
                HeroNode2 pre = temp.pre;
                pre.next = node;
                node.pre = pre;
                node.next = temp;
                temp.pre = node;
            }

        } else {
            HeroNode2 pre = temp.pre;
            pre.next = node;
            node.pre = pre;
            node.next = temp;
            temp.pre = node;
        }

    }

}

class HeroNode2 {
    public int no;
    public String name;
    public String nickname;
    public HeroNode2 next;
    public HeroNode2 pre;

    public HeroNode2(int no, String name, String nickname) {
        this.no = no;
        this.name = name;
        this.nickname = nickname;
    }

    @Override
    public String toString() {
        return "HeroNode{" +
                "no=" + no +
                ", name='" + name + '\'' +
                ", nickname='" + nickname + '\'' +
                '}';
    }
}
